home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / GNU Chess 3.0.3 / src / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-26  |  8.1 KB  |  319 lines  |  [TEXT/CWIE]

  1. /*
  2.   Mac interface for GNU Chess
  3.  
  4.   Revision: 10 Feb 1991
  5.  
  6.   Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  7.   Copyright (c) 1991  Airy ANDRE
  8.  
  9.     expanded game save, list, and restore features
  10.     optional auto-updating of positional information
  11.  
  12.   This file is part of CHESS.
  13.  
  14.   CHESS is distributed in the hope that it will be useful,
  15.   but WITHOUT ANY WARRANTY.  No author or distributor
  16.   accepts responsibility to anyone for the consequences of using it
  17.   or for whether it serves any particular purpose or works at all,
  18.   unless he says so in writing.  Refer to the CHESS General Public
  19.   License for full details.
  20.  
  21.   Everyone is granted permission to copy, modify and redistribute
  22.   CHESS, but only under the conditions described in the
  23.   CHESS General Public License.   A copy of this license is
  24.   supposed to have been given to you along with CHESS so you
  25.   can know your rights and responsibilities.  It should be in a
  26.   file named COPYING.  Among other things, the copyright notice
  27.   and this notice must be preserved on all copies.
  28.   */
  29.  
  30. #include <stdio.h>
  31. #include "math.h"
  32.  
  33. #include "DragMgr.h"
  34. #include "gnuchess.h"
  35. #include "macintf.h"
  36.  
  37. void
  38. GetOpenings ()
  39.  
  40. /*
  41.   Read in the Opening Book file and parse the algebraic notation for a
  42.   move into an unsigned integer format indicating the from and to
  43.   square. Create a linked list of opening lines of play, with
  44.   entry->next pointing to the next line and entry->move pointing to a
  45.   chunk of memory containing the moves. More Opening lines of up to 256
  46.   half moves may be added to gnuchess.book.
  47.   */
  48.  
  49. {
  50.   FILE *fd;
  51.   int c, i, j, side;
  52.   struct BookEntry *entry;
  53.   unsigned short mv, *mp, tmp[100];
  54.  
  55.   if (((fd = fopen (BOOK, "r")) != NULL) ||
  56.       ((fd = fopen ("gnuchess.book", "r")) != NULL))
  57.     {
  58.       Book = NULL;
  59.       i = 0;
  60.       side = white;
  61.       while ((c = parse (fd, &mv, side)) >= 0)
  62.     if (c == 1)
  63.       {
  64.         tmp[++i] = mv;
  65.         side = otherside[side];
  66.       }
  67.     else if (c == 0 && i > 0)
  68.       {
  69.         entry = (struct BookEntry *) malloc (sizeof (struct BookEntry));
  70.         mp = (unsigned short *) malloc ((i + 1) * sizeof (unsigned short));
  71.         if (entry!=NULL && mp!=NULL) {
  72.             entry->mv = mp;
  73.             entry->next = Book;
  74.             Book = entry;
  75.             for (j = 1; j <= i; j++)
  76.               *(mp++) = tmp[j];
  77.             *mp = 0;
  78.             i = 0;
  79.             side = white;
  80.         } else {
  81.             if (entry != NULL) free((Ptr)entry);
  82.             if (mp != NULL) free((Ptr)mp);
  83.         }
  84.       }
  85.       fclose (fd);
  86.     }
  87. }
  88.  
  89.  
  90. int
  91. parse (fd, mv, side)
  92.      FILE *fd;
  93.      short unsigned int *mv;
  94.      short int side;
  95. {
  96.   int c, i, r1, r2, c1, c2;
  97.   char s[100];
  98.   while ((c = getc (fd)) == ' ') ;
  99.   i = 0;
  100.   s[0] = c;
  101.   while (c != ' ' && c != '\n' && c != EOF)
  102.     s[++i] = c = getc (fd);
  103.   s[++i] = '\0';
  104.   if (c == EOF)
  105.     return (-1);
  106.   if (s[0] == '!' || s[0] == ';' || i < 3)
  107.     {
  108.       while (c != '\n' && c != EOF)
  109.     c = getc (fd);
  110.       return (0);
  111.     }
  112.   if (s[4] == 'o')
  113.     if (side == black)
  114.       *mv = 0x3C3A;
  115.     else
  116.       *mv = 0x0402;
  117.   else if (s[0] == 'o')
  118.     if (side == black)
  119.       *mv = 0x3C3E;
  120.     else
  121.       *mv = 0x0406;
  122.   else
  123.     {
  124.       c1 = s[0] - 'a';
  125.       r1 = s[1] - '1';
  126.       c2 = s[2] - 'a';
  127.       r2 = s[3] - '1';
  128.       *mv = (locn(r1, c1) << 8) + locn(r2, c2);
  129.     }
  130.   return (1);
  131. }
  132.  
  133. void
  134. GetGame ()
  135. {
  136.   int i;
  137.   long count;
  138.   SFTypeList list;
  139.   Point where;
  140.   SFReply reply;
  141.   short ref;
  142.   FileHeader header;
  143.   
  144.   where.h = 80; where.v = 90;
  145.   list[0] = 'GCsg';
  146.   SFGetFile(where, "\p", 0L, 1, list, 0L, &reply);
  147.   if (!reply.good) return;
  148.   
  149.   LDelRow(0,0,List);
  150.  
  151.   if ( FSOpen( reply.fName, reply.vRefNum, &ref ) == noErr) {
  152.         SetCursor(*ClockCursor);
  153.         count = sizeof(header);
  154.         FSRead( ref, &count, &header);
  155.         if (header.version == CUR_VERSION && header.signature == SIGNATURE) {
  156.             count = sizeof(drawn);
  157.             FSRead( ref, &count, &drawn);
  158.             count = sizeof(mate);
  159.             FSRead( ref, &count, &mate);
  160.             count = sizeof(towho);
  161.             FSRead( ref, &count, &towho);
  162.             count = sizeof(computer);
  163.             FSRead( ref, &count, &computer);
  164.             count = sizeof(opponent);
  165.             FSRead( ref, &count, &opponent);
  166.             count = sizeof(castld);
  167.             FSRead( ref, &count, castld);
  168.             count = sizeof(TCflag);
  169.             FSRead( ref, &count, TCflag);
  170.             count = sizeof(OperatorTime);
  171.             FSRead( ref, &count, OperatorTime);
  172.             count = sizeof(TimeControl);
  173.             FSRead( ref, &count, &TimeControl);
  174.             count = sizeof(GameCnt);
  175.             FSRead( ref, &count, &GameCnt);
  176.             count = sizeof(color);
  177.             FSRead( ref, &count, color);
  178.             count = sizeof(board);
  179.             FSRead( ref, &count, board);
  180.             count = sizeof(Mvboard);
  181.             FSRead( ref, &count, Mvboard);
  182.             count = 500 * sizeof(struct GameRec);
  183.             FSRead( ref, &count, GameList);
  184.             FSClose( ref );
  185.             InitCursor();
  186.         } else {
  187.             FSClose( ref );
  188.             InitCursor();
  189.             return;
  190.         }
  191.   }
  192.   
  193.   preview = 0;
  194.   InitializeStats ();
  195.   Sdepth = 0;
  196.   for (i=0; i<64; i++) {
  197.       saveColor[i] = color[i];
  198.       saveBoard[i] = board[i];
  199.   }
  200.   for (i=0; i<=GameCnt; i++) {
  201.       algbr(GameList[i].gmove >> 8,GameList[i].gmove & 0xFF,GameList[i].flags);
  202.       AddMove(i, mvstr[(GameList[i].flags & cstlmask) != 0]);
  203.   }
  204.   UpdateDisplay (0, 0, 1, 0, 1, color, board);
  205. }
  206.  
  207. void SaveGame()
  208. {
  209.     Point where;
  210.     SFReply reply;
  211.     long count;
  212.     short outRef;
  213.     FileHeader header;
  214.     Str255 fileName;
  215.     
  216.     where.h = 100; where.v = 50;
  217.     
  218.     header.version = CUR_VERSION;
  219.     header.signature = SIGNATURE;
  220.     GetWTitle(WindBoard, fileName);
  221.     SFPutFile(where, (ConstStr255Param)"", (ConstStr255Param)fileName, 0L, &reply);
  222.     if (!reply.good) return;
  223.     FSDelete(reply.fName, reply.vRefNum);
  224.     Create( reply.fName, reply.vRefNum, 'GCHS', 'GCsg' );
  225.     if ( FSOpen( (ConstStr255Param)reply.fName, reply.vRefNum, &outRef ) == noErr) {
  226.         SetCursor(*ClockCursor);
  227.         count = sizeof(header);
  228.         FSWrite( outRef, &count, &header);
  229.         count = sizeof(drawn);
  230.         FSWrite( outRef, &count, &drawn);
  231.         count = sizeof(mate);
  232.         FSWrite( outRef, &count, &mate);
  233.         count = sizeof(towho);
  234.         FSWrite( outRef, &count, &towho);
  235.         count = sizeof(computer);
  236.         FSWrite( outRef, &count, &computer);
  237.         count = sizeof(opponent);
  238.         FSWrite( outRef, &count, &opponent);
  239.         count = sizeof(castld);
  240.         FSWrite( outRef, &count, castld);
  241.         count = sizeof(TCflag);
  242.         FSWrite( outRef, &count, TCflag);
  243.         count = sizeof(OperatorTime);
  244.         FSWrite( outRef, &count, OperatorTime);
  245.         count = sizeof(TimeControl);
  246.         FSWrite( outRef, &count, &TimeControl);
  247.         count = sizeof(GameCnt);
  248.         FSWrite( outRef, &count, &GameCnt);
  249.         count = sizeof(color);
  250.         FSWrite( outRef, &count, color);
  251.         count = sizeof(board);
  252.         FSWrite( outRef, &count, board);
  253.         count = sizeof(Mvboard);
  254.         FSWrite( outRef, &count, Mvboard);
  255.         count = 500 * sizeof(struct GameRec);
  256.         FSWrite( outRef, &count, GameList);        
  257.         FSClose( outRef );
  258.         InitCursor();
  259.     }
  260. }
  261.  
  262. int FSWriteStr(int ref, char * str)
  263. {
  264.     long count;
  265.     
  266.     count = *str;
  267.     return FSWrite( ref, &count, str); 
  268. }
  269.  
  270. void
  271. ListGame ()
  272. {
  273.     Point where;
  274.     short i, outRef;
  275.     SFReply reply;
  276.     Str255 fileName;
  277.     char bufout[100];
  278.     StringHandle theString;
  279.     
  280.      where.h = 100; where.v = 50;
  281.     
  282.     GetWTitle(WindList, fileName);
  283.     SFPutFile(where, (ConstStr255Param)"", (ConstStr255Param)fileName, 0L, &reply);
  284.     if (!reply.good) return;
  285.     FSDelete(reply.fName, reply.vRefNum);
  286.     Create( reply.fName, reply.vRefNum, 'GCHS', 'TEXT' );
  287.     if ( FSOpen( (ConstStr255Param)reply.fName, reply.vRefNum, &outRef ) == noErr) {
  288.         SetCursor(*ClockCursor);
  289.         theString = GetString(_LISTHEAD_STR);
  290.         HLock((Handle)theString);
  291.         FSWriteStr( outRef, (char *)"\p\r");
  292.           FSWriteStr( outRef, *(char **)theString);
  293.           FSWriteStr( outRef, (char *)"\p         ");
  294.           FSWriteStr( outRef, *(char **)theString);
  295.         FSWriteStr( outRef, (char *)"\p\r");
  296.         HUnlock((Handle)theString);
  297.           for (i = 0; i <= GameCnt; i++)
  298.             {
  299.                 int f,t;
  300.                 
  301.               f = GameList[i].gmove >> 8;
  302.               t = (GameList[i].gmove & 0xFF);
  303.               algbr (f, t, GameList[i].flags);
  304.               if ((i % 2) == 0)
  305.                 FSWriteStr( outRef, (char *)"\p\r");
  306.               else
  307.                 FSWriteStr ( outRef, (char *)"\p         ");
  308.                   sprintf (bufout+1, "%5s  %5d     %2d %7ld %5d", mvstr[0],
  309.                    GameList[i].score, GameList[i].depth,
  310.                    GameList[i].nodes, GameList[i].time);
  311.              *bufout = strlen(bufout+1);
  312.              FSWriteStr( outRef, bufout);
  313.         }
  314.          FSWriteStr ( outRef, (char *)"\p\r\r");
  315.         FSClose( outRef );
  316.         InitCursor();
  317.     }
  318. }
  319.